package ran.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import ran.entity.OptionInfo;
import ran.mapper.OptionMapper;
import ran.service.Optionservice;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-10-08
 * Time: 15:31
 */
@Service
@Slf4j
public class OptionserviceImpl extends ServiceImpl<OptionMapper, OptionInfo> implements Optionservice {

    @Override
    public void importFromExcel(Workbook workbook){

        // === Sheet2: Option ===
        Sheet optionSheet = workbook.getSheetAt(1);
        for (int i = 1; i <= optionSheet.getLastRowNum(); i++) {
            Row row = optionSheet.getRow(i);
            if (row == null) continue;

            OptionInfo o = new OptionInfo();
            o.setQuestionId((long) row.getCell(0).getNumericCellValue());
            o.setOptionKey(row.getCell(1).getStringCellValue().charAt(0));
            o.setOptionContent(row.getCell(2).getStringCellValue());
            o.setCorrect(row.getCell(3).getNumericCellValue() == 1);
            o.setHighRisk(row.getCell(4).getNumericCellValue() == 1);
            o.setErrorReason(row.getCell(5).getStringCellValue());
            this.saveOrUpdate(o,new LambdaQueryWrapper<OptionInfo>().eq(OptionInfo::getOptionKey,o.getOptionKey()).eq(OptionInfo::getQuestionId,o.getQuestionId()));
        }
    }
}
